package com.zlzxm.shoesmarket.view.window.wheelpick;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;

import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;

import java.util.List;

/**
 * CREATE BY zlzxm ON 4/9/21
 */
public class WindowWheelPick extends DialogFragment implements OnItemSelectedListener, View.OnClickListener {

    private WheelView wheelView;

    private List<EntityOption> options;

    private OnOptionSelectListener onOptionSelectListener;


    private WindowWheelPick() {
    }

    public static WindowWheelPick getInstance(){


        return new WindowWheelPick();
    }

    public WindowWheelPick setOptions(List<EntityOption> options) {
        this.options = options;

        return this;
    }

    public WindowWheelPick setOnOptionSelectListener(OnOptionSelectListener onOptionSelectListener) {
        this.onOptionSelectListener = onOptionSelectListener;
        return this;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getContext());

        dialog.setContentView(R.layout.window_wheel_pick);

        dialog.setCanceledOnTouchOutside(false);

        Window window = dialog.getWindow();

        window.setBackgroundDrawable(new ColorDrawable(0));

        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

        window.setLayout(-1,-2 );

        wheelView = ZViewHelp.findById(dialog,R.id.wheelView);

        ZViewHelp.setOnClickListener(dialog,R.id.tvCancel,this);

        ZViewHelp.setOnClickListener(dialog,R.id.tvSure,this);

        ZViewHelp.setOnClickListener(dialog,R.id.rlRoot,this);

        wheelView.setCyclic(false);

        wheelView.setTextSize(20);

        wheelView.setTextColorCenter(ContextCompat.getColor(getContext(),R.color.color99000000));

        wheelView.setTextColorOut(ContextCompat.getColor(getContext(),R.color.color4D000000));

        wheelView.setDividerColor(ContextCompat.getColor(getContext(),R.color.colorE6E6E6));

        wheelView.setLineSpacingMultiplier(2.5f);

        wheelView.setItemsVisibleCount(5);

        if(options !=null) {

            wheelView.setAdapter(new ADOption(options));

            wheelView.setOnItemSelectedListener(this);
        }

        return dialog;
    }


    private void onSure(){

        if(onOptionSelectListener != null){

            onOptionSelectListener.onSelected(options.get(wheelView.getCurrentItem()));
        }


    }


    @Override
    public void onItemSelected(int index) {

    }

    @Override
    public void onClick(View view) {

        if(view.getId() == R.id.tvCancel){

            dismiss();

        }else if(view.getId() == R.id.tvSure) {

            dismiss();

            onSure();

        }else if(view.getId() == R.id.rlRoot){

            dismiss();

        }

    }
}
